# This very makefile (main.mk) is the most recently loaded makefile (lastword MAKEFILE_LIST).
# We use this information as the base directory for further include directives below.
DIR_MAKEFILET := $(dir $(lastword $(MAKEFILE_LIST)))include

RM ?= $(shell which rm) -f
BUILD_ID = $(shell which git >/dev/null && git log -1 --pretty=format:%h || echo "unknown")

DIR_BUILD ?= build
DIR_DIST ?= dist
# e.g. for "debian/rules install"
DESTDIR ?= root

MAKEFILET_CURRENT_VERSION = 0.14.0

include $(DIR_MAKEFILET)/base.mk
include $(DIR_MAKEFILET)/deprecations.mk
include $(DIR_MAKEFILET)/generic.mk
include $(DIR_MAKEFILET)/release.mk
# 'deb-common' is used for simple deb packages and for pypi2deb packages
include $(DIR_MAKEFILET)/deb-common.mk

DISABLE_SHELL_CHECK ?= 0
ifneq ($(DISABLE_SHELL_CHECK),1)
	include $(DIR_MAKEFILET)/shell.mk
endif

# Debian packaging
DIR_DEBIAN_BUILD ?= $(DIR_BUILD)/debian
ifneq ($(wildcard debian/changelog),)
	include $(DIR_MAKEFILET)/deb.mk
endif

# pypi2deb packaging
DIR_PYPI2DEB_BUILD ?= $(DIR_BUILD)/pypi2deb
PYPI2DEB_PACKAGES ?=
ifneq ($(PYPI2DEB_PACKAGES),)
	include $(DIR_MAKEFILET)/pypi2deb.mk
endif

# pypi/python packaging
# DIR_PYTHON_SETUP can be overwritten by the project
DIR_PYTHON_SETUP ?= .
PYTHON_SETUP_FILE = $(DIR_PYTHON_SETUP)/setup.py
PYTHON_PYPROJECT_FILE = $(DIR_PYTHON_SETUP)/pyproject.toml
ifneq ($(wildcard $(PYTHON_SETUP_FILE) $(PYTHON_PYPROJECT_FILE)),)
	include $(DIR_MAKEFILET)/python-common.mk
else ifeq "$(MAKEFILET_ENABLE_PYTHON)" "1"
	# python suppert (e.g. for virtualenv) may be forced via MAKEFILET_ENABLE_PYTHON=1
	include $(DIR_MAKEFILET)/python-common.mk
endif


.PHONY: help
.NOTPARALLEL:
help: help-makefilet

.PHONY: help-makefilet
help-makefilet:
	@echo "Common makefilet targets:"
	@echo "    dist"
	@echo "    help"
	@echo "    install"
	@echo "    report"
	@echo "    style"
	@echo "    test"
	@echo "    upload"
	@echo

.PHONY: clean
clean: clean-makefilet

.PHONY: clean-makefilet
clean-makefilet:
	$(RM) -r "$(DIR_BUILD)"
	# remove installation root directory (only if it is a subdirectory of the project)
	if echo "$(abspath $(DESTDIR))" | grep -q "^$(shell pwd)"; then $(RM) -r "$(DESTDIR)"; fi
	@# A changed DIR_BUILD could cause the makefilet download directory to survive the above
	@# clean operation. We detect the "automatic download" situation based on a non-empty
	@# DIR_MAKEFILET_DOWNLOAD variable.
	if [ -n "$(DIR_MAKEFILET_DOWNLOAD)" ]; then $(RM) -r "$(DIR_MAKEFILET_DOWNLOAD)"; fi

.PHONY: dist-clean
dist-clean: clean
	$(RM) -r "$(DIR_DIST)"


# Reset the default goal. The next processed target (usually in the Makefile of the project) will
# be the default target.
.DEFAULT_GOAL :=
